#include "micro_effect_handle.h"


/******************************由彩虹灯效灯效改的能量灯条*******************************************/

/*******************************************************************
 * @brief    设置ws281x颜色32位
 * @Author   
 * @DateTime 2024-04-01
 * @param    
 * @return   
 *******************************************************************/
uint32_t ws281x_color(uint8_t red, uint8_t green, uint8_t blue)
{
	return red << 16 | green << 8 | blue;
}

/*******************************************************************
 * @brief    
 * @Author   
 * @DateTime 2024-04-01
 * @param    
 * @return   
 *******************************************************************/
uint32_t ws281x_wheel(uint8_t wheelPos) 
{
	wheelPos = 255 - wheelPos;
	if(wheelPos < 85) 
	{
		return ws281x_color(255 - wheelPos * 3, 0, wheelPos * 3);
	}
	if(wheelPos < 170)
	{
		wheelPos -= 85;
		return ws281x_color(0, wheelPos * 3, 255 - wheelPos * 3);
	}
	wheelPos -= 170;
	return ws281x_color(wheelPos * 3, 255 - wheelPos * 3, 0);
}

/*******************************************************************
 * @brief    填充ws281x颜色
 * @Author   
 * @DateTime 2024-04-01
 * @param    
 * @return   
 *******************************************************************/
void ws281x_set_pixel_color(uint16_t n ,uint32_t GRBcolor,u16 length,u8 *buf)
{
	if(n < length)
	{   
       buf[n] = (u8)((GRBcolor>>16)&0xFF);
       buf[n+1] =(u8)((GRBcolor>>8)&0xFF);
       buf[n+2] =(u8)(GRBcolor&0xFF);
	}
}

void micro_light_energy_handle(u8 *buf)
{
    for (u16 i=0; i < get_light_length()*3; i=i+3) 
    {
        ws281x_set_pixel_color(i, ws281x_wheel((i)%255),get_light_length()*3,buf);    //turn every third pixel on
    }
}
